#!usr/bin/python
#-*- coding:utf-8 _*-
"""
@author:xiana
@file: DubboClientNew.py
@time: 2021/12/31  4:19 下午
"""
from dubbo.client import NacosRegister, DubboClient
from dubbo.codec.encoder import Object

from NacosMethods import NacosMethods


class DubboClientNew:

    def __init__(self, host, username, password):
        self.host = host
        self.username = username
        self.password = password

    def callDubbo(self, className, methodName, *args, **param):
        methods = NacosMethods(self.host, self.username, self.password) \
            .get_methods_from_nacos(className, ':')
        nc = NacosRegister(self.host, self.username, self.password)
        methodNames = methods.keys()
        obj = ''
        for method in methodNames:
            if method == methodName:
                obj = methods[method][0]

        queryRequest = Object(obj)

        if args != ():
            queryRequest = args
        elif param != {}:
            for key in param.keys():
                queryRequest[key] = param[key]
        dubbo_cli = DubboClient(className, nacos_register=nc, version=':')
        result = dubbo_cli.call(methodName, queryRequest)
        return result
